<?php

namespace app\portal\controller;

use app\admin\model\Classes;
use app\admin\model\Config;
use app\admin\model\Grade;
use app\admin\model\Paper;
use app\common\controller\AdminBase;
use app\base\model as modelBase;
use app\common\controller\PortalBase;
use think\Exception;

class User extends PortalBase {
  private $model, $modelRole;

  public function __construct() {
    parent::__construct();
    $this->model = new modelBase\User();
    $this->modelRole = new modelBase\Role();
  }

  public function _initialize() {
    parent::_initialize(); // TODO: Change the autogenerated stub
  }

  public function my() {
    $row = $this->model->find(session('user_id'));
    $this->result($row, 1);
  }

  public function resetpw() {
    $id = input('id/d', 0, 'abs');
    $row = $this->model->find($id);
    if (!$row) $this->error(lang('data empty'));
    $defPw = config('system')['default_password'];
    $pw = $this->model->createPassword($defPw, $row['salt']);
    $data = ['password' => $pw];
    try {
      $this->model->save($data, ['id' => $id]);
    } catch (Exception $e) {
      $this->error($e->getMessage());
    }
    $this->success('密码已经重置为' . $defPw);
  }
}
